﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_Type.xaml
    /// </summary>
    public partial class Window_Type : Window
    {
        ProjectGroup projectGroup;
        Type orgType;

        Boolean isNew = false;
        public Window_Type()
        {
            InitializeComponent();
        }

        public void showNew(ProjectGroup pg)
        {
            isNew = true;
            projectGroup = pg;
            TextBlock_Title.Text = "Stovna slag";
            OkButto.Content = "Stovna";
            NameTextBox.Focus();
            NameTextBox.SelectAll();
            NameTextBox.KeyDown += new KeyEventHandler(NameTextBox_PreviewKeyDown);
            this.ShowDialog();
        }

        void NameTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                OkButto_Click(null, null);
                e.Handled = true;
            }
        }

        public void showEdit(ProjectGroup pg, Type t)
        {
            orgType = t;
            projectGroup = pg;
            NameTextBox.Text = t.name;
            TextBlock_Title.Text = "Broyt slag";
            OkButto.Content = "Broyt";
            NameTextBox.Focus();
            NameTextBox.SelectAll();
            NameTextBox.KeyDown += new KeyEventHandler(NameTextBox_PreviewKeyDown);
            this.ShowDialog();
        }

        private void OkButto_Click(object sender, RoutedEventArgs e)
        {
            if(NameTextBox.Text.Trim().Length == 0) 
            {
                MessageBox.Show("Tú skal skriva okkurt í navn feltið!","Feilur",MessageBoxButton.OK,MessageBoxImage.Error);
            }
            else 
            {
                
                if (isNew)
                {
                    Type t = new Type();
                    t.name = NameTextBox.Text;
                    StatusPlanData.statusPlanHandler.saveType(t, projectGroup);
                }
                else
                {
                    orgType.name = NameTextBox.Text;
                    StatusPlanData.statusPlanHandler.saveType(orgType, projectGroup);
                }
                this.Close();
            }
        }

        private void CancelButto_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
